'''
Created on Nov 29, 2010

@author: dexen
'''

import os
import time
import random

from dexen import SlaveTask

import common
from my_ind import MyInd


def sim_perimeter(basefilepath, filename):
    common.enableHouModule()
    import hou
    basefile = os.path.join(basefilepath, filename)
    hou.hipFile.load(basefile)
    hou.node("/obj/geo1/OUT").cook()
    node = hou.node("/obj/geo1/OUT")
    prim = node.geometry().prims()[0]
    att = prim.attribValue("perimeter")
    return att 

def sim_area(basefilepath, filename):
    common.enableHouModule()
    import hou
    basefile = os.path.join(basefilepath, filename)
    hou.hipFile.load(basefile)
    hou.node("/obj/geo1/OUT").cook()
    node = hou.node("/obj/geo1/OUT")
    prim = node.geometry().prims()[0]
    att = prim.attribValue("area")
    return att

def dummy_eval():
    return random.randint(1,100)

class EvalPerimeterTask(SlaveTask):
    def __init__(self):
        pass
    
    def __execute__(self, master_state, inds):
        print "I am in EvalPerimeter"
        for ind in inds:
            assert isinstance(ind, MyInd)
            print os.getcwd()
            ind.perimeter.value = sim_perimeter(common.HOUDINI_BASE_FILE, "eval_perimeter.hipnc")
            #ind.energy.value = dummy_eval()
            ind.archive_data("Perimeter.txt", ind.perimeter.value)
        return inds

class EvalAreaTask(SlaveTask):
    def __init__(self):
        pass
    
    def __execute__(self, master_state, inds):
        print "I am in EvalArea"
        for ind in inds:
            assert isinstance(ind, MyInd)
            ind.area.value = sim_area(common.HOUDINI_BASE_FILE, "eval_area.hipnc")
            #ind.daylight.value = dummy_eval()
            ind.archive_data("Area.txt", ind.area.value)
        return inds

 
    